/**
 *
 * @author Anastasiya
 *
 */

#include "util.h"
#include <direct.h>
#include <windows.h>
#include <direct.h>
#include <tchar.h>
#include <cstdio>
#include <sstream>

using namespace std;

void failure(string message){
	cout << PROGRAM_FAILED << message << endl;
	exit(EXIT_FAILURE);
}

void exit(string message){
	cout << message << endl;
	exit(EXIT_SUCCESS);
}

string numberToString(int number){
	stringstream ss;
	ss << number;
	return ss.str();
}

string numberToString(__int64 number){
	stringstream ss;
	ss << number;
	return ss.str();
}
void createPathIfNotExists(string path){
	string::size_type lastDelimiter = path.find_first_not_of(SLASH);
	if(path.length() == lastDelimiter + 1){
		path.resize(lastDelimiter);
	}

	DWORD attr = GetFileAttributes(path.c_str());
	if(attr == 0xFFFFFFFF){
		int pos = path.find_last_of(SLASH);
		if(pos > 0){
			string parentPath = path.substr(0, pos);
			createPathIfNotExists(parentPath);
			CreateDirectory(path.c_str(), NULL);
		}else if(pos == 0){
			CreateDirectory(path.c_str(), NULL);
		}
	} else if (FILE_ATTRIBUTE_DIRECTORY != attr)
	{  
		SetLastError(ERROR_FILE_EXISTS);
	}
}

string concatPath(string path, string filename){
	unsigned int lastSlash = path.find_last_not_of(SLASH);

	if(path.length() == lastSlash + 1){
		path = path + SLASH;
	}

	return path + filename;
}

